<template>
  <div class="table">
    <el-table
      :data="tableData"
      :loading="loading"
      row-key="id"
      style="width: 100%"
      v-if="refreshTable"
      :default-expand-all="flag"
      :header-cell-style="{
        background: '#F1F1F1',
        'font-size': '14px',
        'font-weight': 600,
        color: '#333333',
      }"
      :cell-style="{
        'font-size': '14px',
        'font-weight': 400,
        color: '#333333',
      }"
    >
      <!-- <el-table-column label="序号" min-width="60" fixed="left">
        <template #default="scope">
          {{ (scope.$index + 1).toString().padStart(2, "0") }}
        </template>
      </el-table-column> -->
      <el-table-column prop="name" label="部门名称" min-width="140" />
      <el-table-column prop="mebNum" label="部门成员数" min-width="100" />
      <el-table-column label="状态" min-width="80">
        <template #default="scope">
          <el-tag :type="scope.row.status ? 'success' : 'info'">{{
            scope.row.status ? "启用" : "禁用"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" min-width="60" />
      <el-table-column prop="createTime" label="创建时间" min-width="180" />
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="scope">
          <el-button
            @click="addData(scope.row)"
            type="primary"
            size="small"
            link
            style="color: #007aff; font-size: 14px"
            >新增</el-button
          >
          <el-button
            @click="editData(scope.row)"
            type="primary"
            size="small"
            link
            style="color: #007aff; font-size: 14px"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.status == true"
            @click="statusData(scope.row)"
            type="primary"
            size="small"
            link
            style="color: #007aff; font-size: 14px"
            >停用</el-button
          >
          <el-button
            v-if="scope.row.status == false"
            @click="statusData(scope.row)"
            type="primary"
            size="small"
            link
            style="color: #007aff; font-size: 14px"
            >启用</el-button
          >
        </template>
      </el-table-column>
      <template #empty>
        <el-empty :image="imgNone" />
      </template>
    </el-table>
  </div>
</template>

<script setup>
import { reactive, ref, defineProps, watch, nextTick } from "vue";
const imgNone = new URL("@/assets/image/imgNone.png", import.meta.url).href;
const emits = defineEmits(["table"]);

const props = defineProps({
  tableData: {
    type: Object,
    default: () => {},
  },
  loading: {
    type: Boolean,
    default: false,
  },
  flag: {
    type: Boolean,
    default: false,
  },
});

// 新增按钮
const addData = (row) => {
  row.operation = "add";
  emits("table", row);
};

// 编辑按钮
const editData = (row) => {
  row.operation = "edit";
  emits("table", row);
};

// 启用/停用按钮
const statusData = (row) => {
  row.operation = "status";
  emits("table", row);
};

const flag = ref(false);

const refreshTable = ref(true);

watch(
  () => props.flag,
  (newVal) => {
    flag.value = props.flag;
    refreshTable.value = false;
    nextTick(() => {
      // 确保DOM更新完成后执行（如果需要的话）
      refreshTable.value = true;
    });
  },
  {
    immediate: true, // 立即执行一次
    deep: true, // 深度监听对象内部变化
  }
);
</script>

<style lang="less" scoped>
.table {
  margin-bottom: 15px;
}
</style>
